package com.simtop.hj.controller;
import com.simtop.hj.common.ResponseMsg;
import com.simtop.hj.common.ServerResponse;
import com.simtop.hj.pojo.TextAnalysisFrequency;
import com.simtop.hj.pojo.*;
import com.simtop.hj.service.ExcelService;
import com.simtop.hj.utils.EmptyUtil;
import com.simtop.hj.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description : TODO用一句话描述此类的作用
 */
@Api(tags = "ExcelDownloadController", description = "Excel 导入下载模板，导出 相关接口")
@RestController
@RequestMapping("/excel")
@CrossOrigin //todo 跨域问题
public class ExcelDownloadController {
    private static final Logger log = LoggerFactory.getLogger(ExcelDownloadController.class);


    @Autowired
    public ExcelService excelService;


    /**
     * 单个文件上传
     * @param file
     * @return
     */
    @ApiOperation(value="接口说明",httpMethod="POST",notes="单个文件上传")
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public ResponseMsg upload(MultipartFile file, Integer sourceId, HttpServletRequest request) {
        ResponseMsg msg = new ResponseMsg();
        try {
            if (file.isEmpty()) {
                msg.setMsg("文件为空");
                msg.setResult(false);
                return msg;
            }
            // 获取文件名
            String fileName = file.getOriginalFilename();
            log.info("上传的文件名为：" + fileName);
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            log.info("文件的后缀名为：" + suffixName);
            //创建一个临时文件，用于暂时存放
            File tmpFile = File.createTempFile("tmp", null);
            //将MultipartFile 转换为 File 临时文件
            file.transferTo(tmpFile);
            //将临时文件转为输入流
            InputStream inputStream = new FileInputStream(tmpFile);
            if (sourceId == 1) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, BrandData.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<BrandData> brandDataList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        BrandData brandData = (BrandData) objectList.get(i);
                        brandDataList.add(brandData);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportBrandData(brandDataList);
                            brandDataList.clear();
                        }
                    }
                    if (brandDataList.size() != 0) {
                        excelService.exportBrandData(brandDataList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 2) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, PreProcessed.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<PreProcessed> processedList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        PreProcessed preProcessed = (PreProcessed) objectList.get(i);
                        processedList.add(preProcessed);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportPreProcessed(processedList);
                            processedList.clear();
                        }
                    }
                    if (processedList.size() != 0) {
                        excelService.exportPreProcessed(processedList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 3) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, WineProcessed.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<WineProcessed> wineProcessedList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        WineProcessed wineProcessed = (WineProcessed) objectList.get(i);
                        wineProcessedList.add(wineProcessed);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportWineProcessed(wineProcessedList);
                            wineProcessedList.clear();
                        }
                    }
                    if (wineProcessedList.size() != 0) {
                        excelService.exportWineProcessed(wineProcessedList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 4) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, WineComment.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<WineComment> wineCommentList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        WineComment wineComment = (WineComment) objectList.get(i);
                        wineCommentList.add(wineComment);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportWineComment(wineCommentList);
                            wineCommentList.clear();
                        }
                    }
                    if (wineCommentList.size() != 0) {
                        excelService.exportWineComment(wineCommentList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 5) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, WineInfo.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<WineInfo> wineInfoList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        WineInfo wineInfo = (WineInfo) objectList.get(i);
                        wineInfoList.add(wineInfo);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportWineInfo(wineInfoList);
                            wineInfoList.clear();
                        }
                    }
                    if (wineInfoList.size() != 0) {
                        excelService.exportWineInfo(wineInfoList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 6) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, WineProduction.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<WineProduction> wineProductionList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        WineProduction wineProduction = (WineProduction) objectList.get(i);
                        wineProductionList.add(wineProduction);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportWineProduction(wineProductionList);
                            wineProductionList.clear();
                        }
                    }
                    if (wineProductionList.size() != 0) {
                        excelService.exportWineProduction(wineProductionList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 7) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, WineNationwideSale.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<WineNationwideSale> wineNationwideSaleList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        WineNationwideSale wineNationwideSale = (WineNationwideSale) objectList.get(i);
                        wineNationwideSaleList.add(wineNationwideSale);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportWineNationwideSale(wineNationwideSaleList);
                            wineNationwideSaleList.clear();
                        }
                    }
                    if (wineNationwideSaleList.size() != 0) {
                        excelService.exportWineNationwideSale(wineNationwideSaleList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 8) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, WineNationwideYield.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<WineNationwideYield> wineNationwideYieldList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        WineNationwideYield wineNationwideYield = (WineNationwideYield) objectList.get(i);
                        wineNationwideYieldList.add(wineNationwideYield);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportWineNationwideYield(wineNationwideYieldList);
                            wineNationwideYieldList.clear();
                        }
                    }
                    if (wineNationwideYieldList.size() != 0) {
                        excelService.exportWineNationwideYield(wineNationwideYieldList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 9) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, PlatformData.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<PlatformData> platformList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        PlatformData platformData = (PlatformData) objectList.get(i);
                        platformList.add(platformData);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportPlatform(platformList);
                            platformList.clear();
                        }
                    }
                    if (platformList.size() != 0) {
                        excelService.exportPlatform(platformList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 10) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, DirtyData.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<DirtyData> dirtyDataList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        DirtyData dirtyData = (DirtyData) objectList.get(i);
                        dirtyDataList.add(dirtyData);
                        if ((i + 1) % 1000 == 0) {
                            excelService.exportDirtyData(dirtyDataList);
                            dirtyDataList.clear();
                        }
                    }
                    if (dirtyDataList.size() != 0) {
                        excelService.exportDirtyData(dirtyDataList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 10) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, DirtyData.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<DirtyData> dirtyDataList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        DirtyData dirtyData = (DirtyData) objectList.get(i);
                        dirtyDataList.add(dirtyData);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportDirtyData(dirtyDataList);
                            dirtyDataList.clear();
                        }
                    }
                    if (dirtyDataList.size() != 0) {
                        excelService.exportDirtyData(dirtyDataList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }if (sourceId == 11) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, StatisticsData.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<StatisticsData> statisticsDataArrayList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        StatisticsData statisticsData = (StatisticsData) objectList.get(i);
                        statisticsDataArrayList.add(statisticsData);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportStatisticsData(statisticsDataArrayList);
                            statisticsDataArrayList.clear();
                        }
                    }
                    if (statisticsDataArrayList.size() != 0) {
                        excelService.exportStatisticsData(statisticsDataArrayList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }if (sourceId == 12) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, StatisticsDataCopy.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<StatisticsDataCopy> statisticsDataCopies = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        StatisticsDataCopy statisticsDataCopy = (StatisticsDataCopy) objectList.get(i);
                        statisticsDataCopies.add(statisticsDataCopy);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportStatisticsDataCopy(statisticsDataCopies);
                            statisticsDataCopies.clear();
                        }
                    }
                    if (statisticsDataCopies.size() != 0) {
                        excelService.exportStatisticsDataCopy(statisticsDataCopies);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 13) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, CustomsRisk.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<CustomsRisk> customsRiskList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        CustomsRisk customsRisk = (CustomsRisk) objectList.get(i);
                        customsRiskList.add(customsRisk);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportCustomsRisk(customsRiskList);
                            customsRiskList.clear();
                        }
                    }
                    if (customsRiskList.size() != 0) {
                        excelService.exportCustomsRisk(customsRiskList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 14) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, PlatformWarn.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<PlatformWarn> platformWarnList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        PlatformWarn platformWarn = (PlatformWarn) objectList.get(i);
                        platformWarnList.add(platformWarn);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportPlatformWarn(platformWarnList);
                            platformWarnList.clear();
                        }
                    }
                    if (platformWarnList.size() != 0) {
                        excelService.exportPlatformWarn(platformWarnList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }if (sourceId == 15) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, Business.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<Business> businessList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        Business business = (Business) objectList.get(i);
                        if(null!=business.getSerialNumber()){
                            businessList.add(business);
                        }
                        if ((i + 1) % 500 == 0) {
                            excelService.exportBusiness(businessList);
                            businessList.clear();
                        }
                    }
                    if (businessList.size() != 0) {
                        excelService.exportBusiness(businessList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }if (sourceId == 16) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, WineFans.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<WineFans> businessList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        WineFans wineFans = (WineFans) objectList.get(i);
                            businessList.add(wineFans);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportWineFans(businessList);
                            businessList.clear();
                        }
                    }
                    if (businessList.size() != 0) {
                        excelService.exportWineFans(businessList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 17) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, JdComment.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<JdComment> jdCommentList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        JdComment jdComment = (JdComment) objectList.get(i);
                        jdCommentList.add(jdComment);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportJdComment(jdCommentList);
                            jdCommentList.clear();
                        }
                    }
                    if (jdCommentList.size() != 0) {
                        excelService.exportJdComment(jdCommentList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 18) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, ProductWord.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<ProductWord> productWordList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        ProductWord productWord = (ProductWord) objectList.get(i);
                        productWordList.add(productWord);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportProductWord(productWordList);
                            productWordList.clear();
                        }
                    }
                    if (productWordList.size() != 0) {
                        excelService.exportProductWord(productWordList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 19) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, ProductDimension.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<ProductDimension> productDimensionList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        ProductDimension productDimension = (ProductDimension) objectList.get(i);
                        productDimensionList.add(productDimension);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportProductDimension(productDimensionList);
                            productDimensionList.clear();
                        }
                    }
                    if (productDimensionList.size() != 0) {
                        excelService.exportProductDimension(productDimensionList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 20) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, ProductEmotion.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<ProductEmotion> productEmotionList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        ProductEmotion productEmotion = (ProductEmotion) objectList.get(i);
                        productEmotionList.add(productEmotion);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportProductEmotion(productEmotionList);
                            productEmotionList.clear();
                        }
                    }
                    if (productEmotionList.size() != 0) {
                        excelService.exportProductEmotion(productEmotionList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 21) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, NegativeProductEmotion.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<NegativeProductEmotion> negativeProductEmotionList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        NegativeProductEmotion negativeProductEmotion = (NegativeProductEmotion) objectList.get(i);
                        negativeProductEmotionList.add(negativeProductEmotion);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportNegativeProductEmotion(negativeProductEmotionList);
                            negativeProductEmotionList.clear();
                        }
                    }
                    if (negativeProductEmotionList.size() != 0) {
                        excelService.exportNegativeProductEmotion(negativeProductEmotionList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 22) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, TextAnalysis.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<TextAnalysis> textAnalysisList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        TextAnalysis textAnalysis = (TextAnalysis) objectList.get(i);
                        textAnalysisList.add(textAnalysis);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportTextAnalysis(textAnalysisList);
                            textAnalysisList.clear();
                        }
                    }
                    if (textAnalysisList.size() != 0) {
                        excelService.exportTextAnalysis(textAnalysisList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 23) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, TextAnalysisFrequency.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<TextAnalysisFrequency> textAnalysisFrequencyList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        TextAnalysisFrequency textAnalysisFrequency = (TextAnalysisFrequency) objectList.get(i);
                        textAnalysisFrequencyList.add(textAnalysisFrequency);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportTextAnalysisFrequency(textAnalysisFrequencyList);
                            textAnalysisFrequencyList.clear();
                        }
                    }
                    if (textAnalysisFrequencyList.size() != 0) {
                        excelService.exportTextAnalysisFrequency(textAnalysisFrequencyList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 24) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, BaiDuNews.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<BaiDuNews> baiDuNewsList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        BaiDuNews baiDuNews = (BaiDuNews) objectList.get(i);
                        baiDuNewsList.add(baiDuNews);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportBaiDuNews(baiDuNewsList);
                            baiDuNewsList.clear();
                        }
                    }
                    if (baiDuNewsList.size() != 0) {
                        excelService.exportBaiDuNews(baiDuNewsList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 25) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, SinaNews.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<SinaNews> sinaNewsList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        SinaNews sinaNews = (SinaNews) objectList.get(i);
                        sinaNewsList.add(sinaNews);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportSinaNews(sinaNewsList);
                            sinaNewsList.clear();
                        }
                    }
                    if (sinaNewsList.size() != 0) {
                        excelService.exportSinaNews(sinaNewsList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
            if (sourceId == 26) {
                List<Object> objectList = ExcelUtils.readExcel(inputStream, WbTopic.class);
                if (objectList == null) {
                    log.info("数据有误");
                    msg.setResult(false);
                    msg.setMsg("数据有误，上传失败");
                    return msg;
                } else if (EmptyUtil.isEmpty(objectList)) {
                    log.info("空");
                    msg.setResult(false);
                    msg.setMsg("数据为空");
                    return msg;
                } else {
                    List<WbTopic> wbTopicList = new ArrayList<>();
                    for (int i = 0; i < objectList.size(); i++) {
                        WbTopic wbTopic = (WbTopic) objectList.get(i);
                        wbTopicList.add(wbTopic);
                        if ((i + 1) % 500 == 0) {
                            excelService.exportWbTopic(wbTopicList);
                            wbTopicList.clear();
                        }
                    }
                    if (wbTopicList.size() != 0) {
                        excelService.exportWbTopic(wbTopicList);
                    }
                    //上传完成 删除临时文件
                    tmpFile.delete();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        msg.setResult(true);
        msg.setMsg("上传成功");
        return msg;
    }


    /**
     * 上传红酒数据分析趋势
     * @param file
     * @param request
     * @return
     */
    @RequestMapping(value = "/uploadWineElement",method = RequestMethod.POST)
    public ServerResponse uploadWineElement(MultipartFile file,HttpServletRequest request) {
        String fileName = file.getOriginalFilename();
        log.info("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        log.info("文件的后缀名为：" + suffixName);
        //创建一个临时文件，用于暂时存放
        List<PriceData> priceDataList = new ArrayList<>();
        File tmpFile = null;
        try {
            tmpFile = File.createTempFile("tmp", null);
            //将MultipartFile 转换为 File 临时文件
            file.transferTo(tmpFile);
            //将临时文件转为输入流
            InputStream inputStream = new FileInputStream(tmpFile);
            List<Object> objectList = ExcelUtils.readExcel(inputStream, PriceData.class);
            if (objectList == null) {
                log.info("数据有误");
                return ServerResponse.createByErrorMsg("数据有误，上传失败");
            } else if (EmptyUtil.isEmpty(objectList)) {
                log.info("空");
                return ServerResponse.createByErrorMsg("数据为空");
            } else {
                for (int i = 0; i < objectList.size(); i++) {
                    PriceData  priceData= (PriceData) objectList.get(i);
                    priceDataList.add(priceData);
                }
                //上传完成 删除临时文件
                tmpFile.delete();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return excelService.exportPriceData(priceDataList);
    }

    /**
     * 下载模板，用于填写导入数据
     * @param request
     * @param response
     */
    @RequestMapping("/downloadExcel")
     public void cooperation (HttpServletRequest request, HttpServletResponse response){
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            String fileName = "红酒数据趋势导入模板";
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
            ExcelUtils.writeExcel(out, ExcelPriceData.class, null);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

//            @RequestMapping("/downloadExcel2")
//            public void cooperation2 (HttpServletRequest request, HttpServletResponse response, Integer sourceId){
//                log.info("sourceId" + sourceId);
//                //  @RequestBody Integer sourceId
//                JSONObject jsonObj = new JSONObject(sourceId);
//                Integer downSourceId = sourceId;
//                ServletOutputStream out = null;
//                try {
//                    out = response.getOutputStream();
//                    response.setContentType("multipart/form-data");
//                    response.setCharacterEncoding("utf-8");
//                    String fileName = "";
//                    switch (downSourceId) {
//                        case 1:
//                            fileName = "微博导入模板";
//                            break;
//                        case 2:
//                            fileName = "微信导入模板";
//                            break;
//                        case 3:
//                            fileName = "新闻导入模板";
//                            break;
//                        case 4:
//                            fileName = "招聘导入模板";
//                            break;
//                        case 5:
//                            fileName = "专利导入模板";
//                            break;
//                        case 6:
//                            fileName = "企业官网导入模板";
//                            break;
//                        case 7:
//                            fileName = "社区论坛导入模板";
//                            break;
//                        case 8:
//                            fileName = "企业年报导入模板";
//                            break;
//                        case 9:
//                            fileName = "工商信息导入模板";
//                            break;
//                    }
//                    response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
//                    switch (downSourceId) {
//                        case 1:
//                            ExcelUtils.writeExcel(out, WeiBo.class, null);
//                            break;
//                        case 2:
//                            ExcelUtils.writeExcel(out, WeChat.class, null);
//                            break;
//                        case 3:
//                            ExcelUtils.writeExcel(out, News.class, null);
//                            break;
//                        case 4:
//                            ExcelUtils.writeExcel(out, Recruitment.class, null);
//                            break;
//                        case 5:
//                            ExcelUtils.writeExcel(out, Patent.class, null);
//                            break;
//                        case 6:
//                            ExcelUtils.writeExcel(out, CompanyWebsite.class, null);
//                            break;
//                        case 7:
//                            ExcelUtils.writeExcel(out, CommunityForum.class, null);
//                            break;
//                        case 8:
//                            ExcelUtils.writeExcel(out, CorporateAnnualReports.class, null);
//                            break;
//                        case 9:
//                            ExcelUtils.writeExcel(out, BusinessInformation.class, null);
//                            break;
//                    }
//                    out.flush();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                } finally {
//                    if (out != null) {
//                        try {
//                            out.close();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
        }